package com.yyh.common;

import com.yyh.common.util.MapModelUtils;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author xuan
 * @date 2021/12/7
 */
public class Common
{
    /** 格式 yyyy-MM-dd HH:mm:ss */
    public static String currentTime() {
        String pattern = "yyyy-MM-dd HH:mm:ss";
        return new SimpleDateFormat(pattern).format(System.currentTimeMillis());
    }

    /** 线程休眠，单位秒 */
    public static void sleep(int seconds) {
        try {
            long millis = seconds * 1000;
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            throw new RuntimeException(e.getMessage(), e.getCause());
        }
    }

    /** 模型转换 */
    /** 对象转map */
    public static Map<String, Object> toMap(Object object) {
        try {
            return MapModelUtils.toMap(object);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e.getCause());
        }
    }

    /** 将Map<String, ?> 转为 Map<String, String> */
    public static Map<String, String> stringifyMapValue(Map<String, ?> map) {
        if (null == map) {
            return null;
        } else {
            HashMap<String, String> result = new HashMap<>();

            Iterator<? extends Map.Entry<String, ?>> iterator = map.entrySet().iterator();

            while (iterator.hasNext()) {
                Map.Entry<String, ?> entry = iterator.next();
                if (null != entry.getKey() && null != entry.getValue()) {
                    result.put(entry.getKey(), String.valueOf(entry.getValue()));
                }
            }

            return result;
        }
    }
}
